package com.project.imageinterference.vo;

import com.project.imageinterference.exception.BusinessException;

/**
 * ｛Result｝
 *
 * @author x
 */
public class Result<T> {

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 提示消息
     */
    private String msg;

    /**
     * 响应数据
     */
    private T data;

    /**
     * 操作结果标志位
     */
    private boolean isSuccess;

    public Result() {
    }

    /**
     * 私有化构造函数
     */
    private Result(ResultCode resultCode, T data) {
        this.code = resultCode.getCode();
        this.msg = resultCode.getMessage();
        this.data = data;
        this.isSuccess = true;
    }

    /**
     * 私有化构造函数
     */
    private Result(ResultCode resultCode, T data, boolean isSuccess) {
        this.code = resultCode.getCode();
        this.msg = resultCode.getMessage();
        this.data = data;
        this.isSuccess = isSuccess;
    }

    /**
     * 私有化构造函数
     */
    private Result(ResultCode resultCode, T data, String message, boolean isSuccess) {
        this.code = resultCode.getCode();
        this.msg = message;
        this.data = data;
        this.isSuccess = isSuccess;
    }

    private Result(Integer resultCode, T data, String message, boolean isSuccess) {
        this.code = resultCode;
        this.msg = message;
        this.data = data;
        this.isSuccess = isSuccess;
    }

    public static Result<Void> success() {
        return new Result<>(ResultCode.SUCCESS, null);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(ResultCode.SUCCESS, data);
    }

    public static <T> Result<T> failure() {
        return new Result<>(ResultCode.INTERNAL_SERVER_ERROR, null, false);
    }

    public static <T> Result<T> failure(ResultCode resultCode) {
        return new Result<>(resultCode, null);
    }

    public static <T> Result<T> failure(String message) {
        return new Result<>(ResultCode.INTERNAL_SERVER_ERROR, null, message, false);
    }

    public static <T> Result<T> result(ResultCode resultCode, T data) {
        return new Result<>(resultCode, data);
    }

    public static <T> Result<T> defineError(BusinessException de) {
        return new Result<>(de.getCode(), null, de.getMessage(), false);
    }

    public static <T> Result<T> otherError(ResultCode errorNum) {
        return new Result<>(errorNum.getCode(), null, errorNum.getMessage(), false);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    /**
     * @return the msg
     */
    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    /**
     * @return the data
     */
    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * @return 返回 isSuccess.
     */
    public boolean isSuccess() {
        return isSuccess;
    }

    public void setSuccess(boolean success) {
        isSuccess = success;
    }
}
